"Cherry_man1" (Cherry_man1)
12/02/2014 at 14:13 • Filed to: Programming | 2 | 2 |
I am doing the #1 on this thing and its stumping me.
Any insight?
Language python: 3.4.1
!!!error: Indecipherable SUB-paragraph formatting!!!
Have a ford GT for help.
mattc993
> Cherry_man1
12/02/2014 at 14:21 | 2 |
Assuming you want to return the non-orange (non-favorite colors) to the bottom of the stack, something like this will be your eatFavoriteCandy method:
currentPez = self.s.pop()
if currentPez != "Orange"
self.s.push(currentPez)
else
return
That's probably not code complete or syntactically correct, but hopefully you get the idea? The fact that the example is a pez dispenser is not coincidental. Think of how one works in real life, and you have the concept of a stack in CS. Push items to the bottom, or pop them off the top.
Redline
> mattc993
12/02/2014 at 15:45 | 0 |
That's not how a stack works in CS. You both put/push or take/pop items from the same end of the stack. You are thinking of a FIFO queue. Stacks operate via LIFO, Last In First Out.
To solve problem number 1 you can exploit the code in the displayContents method. Create a new stack (like s2), pop everything out of the self.s stack containing the candy onto the new stack s2 unless it is orange.
In pseudocode:
s2 = LinkedStack()
while self.s stack is not empty
currentPez = self.s.pop()
if currentPez != "Orange"
s2.push(currentPez)
#now candy is stored in stack s2 in the reverse order. Move it back to original stack
while not s2.isEmpty()
self.s.push(s2.pop())